package com.todo.content.application.frontend.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.todo.common.common.entity.response.BizResponse;
import com.todo.common.common.enums.BizErrorCodeEnum;
import com.todo.common.common.exception.BizException;
import com.todo.content.application.base.apollo.ContentApollo;
import com.todo.content.application.convert.ContentConvert;
import com.todo.content.application.entity.bo.ContentTopicBO;
import com.todo.content.application.frontend.service.ContentTopicAggService;
import com.todo.content.application.service.ContentTopicService;
import com.todo.content.iface.frontend.request.ContentTopicBaseListParam;
import com.todo.content.iface.frontend.request.QueryContentTopicDetailByIdParam;
import com.todo.content.iface.frontend.request.RecommendContentTopicListParam;
import com.todo.content.iface.frontend.response.ContentTopicBaseVO;
import com.todo.content.iface.frontend.response.ContentTopicDetailVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * (ContentTopic)表服务实现类
 *
 * @author zhangwenxiang
 * @since 2024-03-04 00:19:46
 */
@Slf4j
@Service("contentTopicAggService")
public class ContentTopicAggServiceImpl implements ContentTopicAggService {

    @Resource
    private ContentTopicService contentTopicService;
    @Resource
    private ContentApollo contentApollo;

    @Override
    public BizResponse<List<ContentTopicBaseVO>> queryContentTopicBaseList(ContentTopicBaseListParam param) {
        if (param == null || !param.baseCheck()) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        List<ContentTopicBO> contentTopicBaseResponses = contentTopicService.searchContentTopicByName(param.getTopicKeyword());
        if (CollectionUtils.isEmpty(contentTopicBaseResponses)) {
            return BizResponse.success();
        }
        return BizResponse.success(ContentConvert.convertToContentTopicBaseVOList(contentTopicBaseResponses));
    }

    @Override
    public BizResponse<List<ContentTopicBaseVO>> baseRecommendContentTopicList(RecommendContentTopicListParam param) {
        if (param == null || !param.baseCheck()) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        //TODO 增加针对推荐话题的二级缓存配置30min/24h，如果apollo调整回调修改
        List<Long> topicIdList = contentApollo.queryRecommendContentTopicIdList();
        if (CollectionUtils.isEmpty(topicIdList)) {
            log.warn("ContentTopicAggService#baseRecommendContentTopicList 推荐话题列表查询完成，配置数据为空，默认返回空数据");
            return BizResponse.success();
        }
        List<ContentTopicBO> contentTopicBOList = contentTopicService.queryContentTopicByIdList(topicIdList);
        return BizResponse.success(ContentConvert.convertToContentTopicBaseVOList(contentTopicBOList));
    }

    @Override
    public BizResponse<ContentTopicDetailVO> queryContentTopicDetailById(QueryContentTopicDetailByIdParam param) {
        if (param == null || !param.baseCheck()) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        ContentTopicBO contentTopicBO = contentTopicService.queryContentTopicById(Long.valueOf(param.getTopicId()));
        if (contentTopicBO == null) {
            log.warn("ContentTopicAggService#queryContentTopicDetailById 根据话题查询话题详情完成，数据为空 入参：【{}】", JSONObject.toJSONString(param));
            return BizResponse.success();
        }
        return BizResponse.success(ContentConvert.convertToContentTopicDetailVO(contentTopicBO));
    }
}
